#!/bin/bash
PROG=@CMAKE_INSTALL_PREFIX@/llvmbin/ld.lld
POLICY="cache_size_bytes=2g:prune_interval=1m"
FLAGS="-m @ld_m_flag@"
FLAGS+=" --no-insert-timestamp --major-os-version=10 --minor-os-version=0 --major-subsystem-version=6 --minor-subsystem-version=2 --file-alignment=4096 --disable-auto-import --disable-runtime-pseudo-reloc -mllvm -disable-auto-upgrade-debug-info -s -Xlink=-debug:none,nodwarf,nosymtab --stack=1048576,1048576 --thinlto-jobs=all"
unset FLAGS_INIT

if [ "$_IS_CONFIGURE" == "1" ]; then
    SKIP_OPT="-O0 --lto-O0 --lto-CGO0 -mllvm -import-instr-limit=0 --no-guard-cf --build-id=none"
else
    FLAGS+=" --thinlto-cache-dir=@MINGW_INSTALL_PREFIX@/thinlto --thinlto-cache-policy=$POLICY"
    FLAGS+=" -O3 --lto-O3 --lto-CGO3 -mllvm -import-instr-limit=175 -mllvm -import-hot-multiplier=2 --gc-sections --icf=all -Xlink=-release -Xlink=-opt:lldtailmerge -Xlink=-cetcompat -Xlink=-cetcompatstrict -Xlink=-pdbsourcepath:C:\\ -Xlink=-pdbaltpath:%_PDB%"
    if [ "$_PDB_GENERATE" == "1" ]; then
        FLAGS+=" -Xlink=-debug:ghash,nodwarf,nosymtab --pdb="
    fi
    if [ "$_LTO_ENABLED" != "0" ]; then
        FLAGS+=" @llvm_mllvm@ @polly@"
        if [ "@TARGET_CPU@" == "aarch64" ]; then
            FLAGS+=" -mllvm -polly=false"
        fi
    fi
    if [ "@LLVM_ENABLE_PGO@" == "GEN" ] || [ "@LLVM_ENABLE_PGO@" == "CSGEN" ]; then
        export LLVM_PROFILE_FILE="@LLVM_PROFILE_DATA_DIR@/lld-%m.profraw" #enable profraw output only when CONF!=1
        export LLD_IN_TEST=1 #https://github.com/llvm/llvm-project/issues/52861
    fi
    if [ "@MARCH_HAS_AVX@" == "ON" ]; then
        FLAGS+=" -mllvm -x86-use-vzeroupper=false"
        if [ "@MARCH_HAS_AVX512@" == "ON" ]; then
            FLAGS+=" -mllvm -x86-use-fsrm-for-memcpy"
        fi
    fi
    FLAGS+=" @LLD_FLAGS@ $EXTRA_LDFLAGS"
    FLAGS_INIT="@MINGW_INSTALL_PREFIX@/lib/libopenlibm.a @MINGW_INSTALL_PREFIX@/lib/libsleefgnuabi.a"
fi

"$PROG" $FLAGS_INIT "$@" $FLAGS $SKIP_OPT
